package json;

import util.BannerUtil;
import util.DowLoadUtil;

import java.io.*;
import java.util.Scanner;

/**
 * 命令行
 */

public class ParseJson {
    //输入关键字
    private static String antistop;
    //编码关键字
    private static String encode;
    private static int i;
    private static int name = 1;
    private static InputStream inputStream = null;
    private static FileOutputStream outputStream = null;
    //下载路径
    private static String path;
    //删除文件数量
    private static int c = 0;
    //下载图片的颜色
    private static String color = "";
    //下载图片尺寸
    private static String size = "";

    private static Scanner scanner = new Scanner(System.in);

    //选择图片尺寸功能
    public static void selectSize() {
        System.out.println("                                                            请输入你需要的尺寸");
        System.out.println("                                                            1、全部尺寸\n" +
                "                                                            2、特大尺寸\n" +
                "                                                            3、大尺寸\n" +
                "                                                            4、中尺寸\n" +
                "                                                            5、小尺寸");
        size = scanner.next();
        switch (size) {
            case "1":
                size = "0";
                break;
            case "2":
                size = "9";
                break;
            case "3":
                size = "3";
                break;
            case "4":
                size = "2";
                break;
            case "5":
                size = "1";
                break;
            default:
                System.out.println("                                                            请选择正确选项");
                selectSize();
                break;
        }
    }


    //主界面run
    public static void run() {
        String kai = scanner.next();
        switch (kai) {
            case "1":
                operate();
                break;
            case "2":
                System.out.println("                                                            欢迎下次使用");
                System.exit(0);
                break;
        }


    }

    //操作
    public static void operate() {
        selectColor();
        selectSize();
        System.out.println("                                                            请输入关键字");
        antistop = scanner.next();
        System.out.println("                                                            请输入下载路径");
        path = scanner.next();
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
            System.out.println("                                                            创建文件夹成功");
        }
        System.out.println("                                                            请输入下载页数/每页30张");
        i = (scanner.nextInt() - 1) * 30;
    }


    //选择图片颜色功能
    public static void selectColor() {
        System.out.println("                                                            请输入你需要的颜色");
        System.out.println(
                "                                                            1、全部颜色\n" +
                        "                                                            2、黑色\n" +
                        "                                                            3、粉色\n" +
                        "                                                            4、蓝色\n" +
                        "                                                            5、红色\n" +
                        "                                                            6、黄色\n" +
                        "                                                            7、紫色\n" +
                        "                                                            8、绿色\n" +
                        "                                                            9、青色\n" +
                        "                                                            10、橙色\n" +
                        "                                                            11、棕色");
        color = scanner.next();
        switch (color) {
            case "1":
                color = "0";
                break;
            case "2":
                color = "512";
                break;
            case "3":
                color = "64";
                break;
            case "4":
                color = "16";
                break;
            case "5":
                color = "1";
                break;
            case "6":
                color = "2";
                break;
            case "7":
                color = "32";
                break;
            case "8":
                color = "4";
                break;
            case "9":
                color = "8";
                break;
            case "10":
                color = "256";
                break;
            case "11":
                color = "128";
                break;
            default:
                System.out.println("                                                            请输入正确选项");
                selectColor();
                break;
        }
    }

/*
    //下载
    public static void dowload() {
        try {
            encode = URLEncoder.encode(antistop, "utf-8");
        } catch (UnsupportedEncodingException e) {
            System.out.println("解码失败");
        }
        for (int page = 0; page <= i; page += 30) {
            String urls = "https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=" + encode
                    + "&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=" + size + "&ic=" + color + "&hd=&latest=&copyright=&word=" + encode
                    + "&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn="
                    + page + "&rn=30&gsm=1e&1588511270025=";
            try {
                URL url = new URL(urls);
                try {
                    URLConnection urlConnection = url.openConnection();
                    InputStream in = urlConnection.getInputStream();
                    InputStreamReader reader = new InputStreamReader(in);
                    BufferedReader bufferedReader = new BufferedReader(reader);
                    String s = null;
                    StringBuffer buffer = new StringBuffer();
                    while ((s = bufferedReader.readLine()) != null) {
                        buffer.append(s);
                    }
                    ObjectMapper objectMapper = new ObjectMapper();
                    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
                    Image image = objectMapper.readValue(buffer.toString(), Image.class);
      */
/*              image.getData().stream().forEach(i -> {
                        if (i.getThumbURL() != null) {
                            try {
                                //将目标url在ParseImageUtil类的parse解析解析
                                inputStream = new URL(ParseImageUtil.parse(i.getObjURL())).openConnection().getInputStream();
                                String fileName = ParseImageUtil.getFileName(i.getObjURL());
                                if (fileName.indexOf("?") != -1){
                                   fileName = fileName.substring(0,fileName.indexOf("?"));
                                }


                                File file = new File(path + "\\" +fileName);
                                System.out.println(file.getName());
                                outputStream = new FileOutputStream(file);
                                byte[] bytes = new byte[1024];
                                int len;
                                while ((len = inputStream.read(bytes)) != -1) {
                                    //下载图片
                                    outputStream.write(bytes, 0, len);
                                }
                                System.out.println("                                                            下载第" + name + "张图片-------------完成-------------");
                                name++;
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                //释放资源
                                if (outputStream != null) {
                                    try {
                                        outputStream.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if (inputStream != null) {
                                    try {
                                        inputStream.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    });*/
/*

                    System.out.println("                                             [系统-------------下载系统启动-------------系统]");
                    for (I i : image.getData()) {
                        //解析objurl
                        if (i.getObjURL() != null) {
                            String parse = ParseImageUtil.parse(i.getObjURL());
                            try {
                                //将目标url在ParseImageUtil类的parse解析解析
                                HttpURLConnection connection =  (HttpURLConnection)new URL(parse).openConnection();
                                //设置连接时间5秒
                                connection.setConnectTimeout(5000);
                                inputStream = connection.getInputStream();
                                String fileName = ParseImageUtil.getFileName(i.getObjURL());
                                if (fileName.indexOf("?") != -1) {
                                    fileName = fileName.substring(0, fileName.indexOf("?"));
                                }
                                if (fileName.indexOf("@") != -1) {
                                    fileName = fileName.substring(0, fileName.indexOf("@"));
                                }
                                File file = new File(path + "\\" + fileName);
                                outputStream = new FileOutputStream(file);
                                byte[] bytes = new byte[1024];
                                int len;
                                while ((len = inputStream.read(bytes)) != -1) {
                                    //下载图片
                                    outputStream.write(bytes, 0, len);
                                }
                                System.out.println("                                     [目标-------------" + file.getName() + "-------------目标]");
                                System.out.println("                                             [完成-------------下载第" + name + "张图片-------------完成]");
                                name++;
                            } catch (IOException e) {
                                e.printStackTrace();
                            } finally {
                                //释放资源
                                if (outputStream != null) {
                                    try {
                                        outputStream.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                if (inputStream != null) {
                                    try {
                                        inputStream.close();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }

                } catch (IOException e) {
                    System.out.println("                                                            连接失败");

                }
            } catch (MalformedURLException e) {
                System.out.println("                                                            获取不到该URL");

            }
        }

        File file = new File(path);
        File[] list = file.listFiles();
        System.out.println("                                          [系统-------------无效文件删除系统启动-------------系统]");
        for (File file1 : list) {
            if (file1.length() >= 1024) {
                if (file1.getName().indexOf(".jpg") != -1
                        || file1.getName().indexOf(".jpeg") != -1
                        || file1.getName().indexOf(".gif") != -1
                        || file1.getName().indexOf(".png") != -1
                        || file1.getName().indexOf(".bmp") != -1) {
                    continue;
                }else {
                    file1.delete();
                    c++;
                    System.out.println("                                             [无效文件]" + file1.getName() + "删除");
                }
            } else {
                file1.delete();
                c++;
                System.out.println("                                             [无效文件]" + file1.getName() + "删除");
            }
        }
        System.out.println("                                                            下载完成");
        System.out.println("                                                         [有效文件]:"+(name-c)+"张");
    }
*/

    public static void main(String[] args) throws Exception {
        System.out.println("                                                          欢迎使用百度爬虫图片程序");
        BannerUtil.print();
        System.out.println("                                                            1、操作  2、退出");
        run();
        DowLoadUtil.commandLineDownload(antistop,encode,outputStream,inputStream,path,size,color,name,c,i);
    }
}
